package app;

import cn.hutool.core.io.FileUtil;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.StringUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

/**
 * <p>
 * 启动参数：<br/>
 * -Dcustom.config.enabled 是否启用自定义配置，默认 false 不启用。<br/>
 * -Dcustom.config.path 自定义配置的路径，默认项目下 local-dev.properties。<br/>
 * </p>
 *
 *
 * @author mh.z
 */
@MapperScan("app.**.mapper")
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = {"app"})
public class AppMain {
    private static final Logger logger = LoggerFactory.getLogger(AppMain.class);

    private static final String SYSTEM_PROP_CUSTOM_CONFIG_ENABLED = "custom.config.enabled";
    private static final String SYSTEM_PROP_CUSTOM_CONFIG_PATH = "custom.config.path";
    private static final String SYSTEM_PROP_USER_DIR = "user.dir";
    private static final String DEFAULT_CUSTOM_CONFIG_FILE_NAME = "local-dev.properties";
    private static final String PROPERTY_PLACEHOLDER_USER_DIR = "${user.dir}";

    /**
     * 应用启动入口
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String customConfigEnabled = System.getProperty(SYSTEM_PROP_CUSTOM_CONFIG_ENABLED);
        String customConfigPath = System.getProperty(SYSTEM_PROP_CUSTOM_CONFIG_PATH);

        // 初始系统参数
        if ("true".equals(customConfigEnabled)) {
            if (StringUtil.isEmpty(customConfigPath)) {
                customConfigPath = System.getProperty(SYSTEM_PROP_USER_DIR) +
                        File.separator + DEFAULT_CUSTOM_CONFIG_FILE_NAME;
            }

            initSystemProperties(customConfigPath);
        }

        // 启动应用
        SpringApplication.run(AppMain.class, args);

        logger.info("Server running...");
    }

    /**
     * 初始系统参数
     *
     * @param configPath
     * @throws Exception
     */
    public static void initSystemProperties(String configPath) throws Exception {
        CheckUtil.notNull(configPath, "configPath null");
        Properties properties = new Properties();
        String userDir = System.getProperty(SYSTEM_PROP_USER_DIR)
                .replace("\\", "/");

        // 加载配置
        try (InputStream inputStream = FileUtil.getInputStream(configPath)) {
            properties.load(inputStream);
        }

        Set<Object> keySet = properties.keySet();
        // 设置系统参数
        for (Object key : keySet) {
            String propertyName = (String) key;
            String propertyValue = properties.getProperty(propertyName)
                    .replace(PROPERTY_PLACEHOLDER_USER_DIR, userDir);
            System.setProperty(propertyName, propertyValue);
        }
    }

}
